package com.yunc.common.tio;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.tio.server.ServerGroupContext;
import org.tio.websocket.server.WsServerStarter;

/**
 * @author tao cong cong
 * @create 2018/11/24 下午9:25
 */
@Component
public class IOApplicationRunner implements ApplicationRunner {

    private WsServerStarter wsServerStarter;
    private ServerGroupContext serverGroupContext;

    public static final String PROTOCOL_NAME = "showcase";
    public static final int HEARTBEAT_TIMEOUT = 1000 * 60;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        wsServerStarter = new WsServerStarter(8888, ShowcaseWsMsgHandler.me);
        serverGroupContext = wsServerStarter.getServerGroupContext();

        serverGroupContext.setName(PROTOCOL_NAME);
        //设置心跳超时时间
        serverGroupContext.setHeartbeatTimeout(HEARTBEAT_TIMEOUT);
        wsServerStarter.start();
    }

    public ServerGroupContext getServerGroupContext() {
        return serverGroupContext;
    }

    public WsServerStarter getWsServerStarter() {
        return wsServerStarter;
    }
}
